package DAO.Post;

import DAO.Usuario.HibernateUtil;
import Model.Post;
import com.google.gson.Gson;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.hibernate.Session;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author pedro
 */
/**
 *
 * @author pedro
 */
@Path("/getPost")
public class PostDao {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public Response sayHello(Post post) {
        Session session = null ;
        HibernateUtil.beginTransaction();
        List<Post> lista = null;
        String result = null;
        Gson gson = new Gson();
        try {
            session= HibernateUtil.getSession();
            lista = session.createQuery("From User WHERE cod=? ")
                    .setString(0, post.getCod().toString())
                    .list();
            HibernateUtil.commitTransaction();
            result = gson.toJson(lista.get(0));

        } catch (Exception e) {
            System.out.println(e);
            result="Error";
            session.getTransaction().rollback();
            
        } finally {
            HibernateUtil.closeSession();
        }
        return Response.status(201).entity(result).build();

    }
}
